package cn.dragon.eatingfish;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public abstract class Drop extends SeaObject {
    private int xSpeed;
    private int ySpeed;

    public Drop() {
        Random rand = new Random(); //随机数对象
        setX(rand.nextInt(World.WIDTH) + 1);
        setY(0);
        setHeight(27);
        setWidth(40);
        xSpeed = rand.nextInt(3) + 1;
        ySpeed = rand.nextInt(3) + 1;
    }

    public abstract ImageIcon getImage();

    public void move() {
        setX(getX() + xSpeed);
        setY(getY() + ySpeed);
        if (getX() < 0 || getX() > World.WIDTH) {
            xSpeed *= -1;
        }
    }

    public void paintImage(Graphics g) {
        this.getImage().paintIcon(null, g, getX(), getY());

    }

    public boolean isHit(SeaObject other) {
        int x1 = getX() - other.getWidth();  //x1:主角的x-npc的宽
        int x2 = getX() + getWidth() / 2;   //x2:潜艇的x+潜艇的宽
        int y1 = getY() - other.getHeight(); //y1:潜艇的y-炸弹的高
        int y2 = getY() + getHeight() / 2;  //y2:潜艇的y+潜艇的高
        int x = other.getX(); //x:npc的x
        int y = other.getY(); //y:npc的y

        return x >= x1 && x <= x2
                &&
                y >= y1 && y <= y2; //x在x1与x2之间，并且，y在y1与y2之间，即为撞上了
    }

    //
    public void goDead() {
        setState(DEAD);
    }

    public boolean isLive() {
        return getState() == LIVE;
    }

    public boolean isDead() {
        return getState() == DEAD;
    }

    public boolean isOutOfaction() {
        return getY() > World.HEIGHT;
    }

}
